<template>
  <div>
    <el-button class="add" type="primary"  icon="el-icon-plus" @click="add">新增</el-button>
    <el-tabs @tab-click="handleClick" v-model="activeName">
      <el-tab-pane label="上架" name="first">
      </el-tab-pane>
      <el-tab-pane label="下架" name="sencond">
      </el-tab-pane>
    </el-tabs>

      <el-table :data="bannerList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="180">
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="img" label="图片地址"> </el-table-column>
        <el-table-column prop="url" label="跳转路径"> </el-table-column>
        <el-table-column prop="weight" label="排序"> </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            {{scope.row.type === 0 ? '上架' : '下架'}}
          </template>
        </el-table-column>
        <el-table-column prop="creatTime" label="创建时间" width="180">
        </el-table-column>
         <el-table-column label="操作" width="180">
        <template slot-scope="scope">
           <el-button
            v-if="activeName === 'first'"
            @click="toggle(scope.row.id,scope)"
            type="text"
            size="small"
            class="eidt">
            {{activeName === 'first' ? '下架' : ''}}
            <!-- 修改 -->
          </el-button>
           <el-button
            @click="edit(scope.row.id,scope)"
            type="text"
            size="small"
            class="eidt">
            修改
          </el-button>
           <!-- <el-button
            class="del"
            @click="del(scope.row.id,scope)"
            type="text"
            size="small">
            删除
          </el-button> -->
        </template>
      </el-table-column>
      </el-table>
      <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          :total="total">
      </el-pagination>  

     <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="标题" prop="title" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片地址" prop="img" :label-width="formLabelWidth">
           <el-input v-model="form.img" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="跳转链接" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="form.weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否上架" prop="type" :label-width="formLabelWidth">
          <el-select v-model="form.type" :disabled="!isAdd" placeholder="请选择">
            <el-option label="上架"  :value="0"></el-option>
            <el-option label="下架"  :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" v-if="isAdd" @click="addData">确 定</el-button>
        <el-button type="primary" v-if="!isAdd" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeName:'first',
      bannerList: [],
      dialogFormVisible: false,
      title:'',
      total:null,
      currentPage:1,
      form: {
        title: '',
        url: '',
        img: '',
        weight: '',
        type:''
      },
      rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ],
          img:[
            { required: true, message: '请输入图片地址', trigger: 'blur' }
          ],
          type:[
            { required: true, message: '请选择是否下架', trigger: 'blur' }
          ]
      },
      formLabelWidth: '120px',
      isAdd: true,
      id: ''
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.getBannerList(val)
    },
    handleClick(tab, event) {
      this.currentPage = 1
      this.getBannerList(this.currentPage)
    },
    getBannerList(page){
      let type = this.activeName === 'first' ? 0 : 1
      // let token = localStorage.getItem('sessionId')
      let pra = {
        currentPage:page,
        pageSize: 10,
        query:{
          type:type
        }
      }
      this.$axios.post(`${this.$apiUrl}/gamemall/banner/get`, pra)
        .then(res => {
          this.bannerList = res.data.list
          this.total = res.data.total
        })
        .catch(error => {
          console.log(error);
        });
    },
    add(){
      this.title = '新增'
      this.isAdd = true
      this.form = {
        title: '',
        url: '',
        img: '',
        weight: '',
        type:''
      }
      this.dialogFormVisible = true
    },
    addData(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let pra = this.form
          this.$axios
          .post(`${this.$apiUrl}/gamemall/banner/add`, pra)
          .then(res => {
            this.dialogFormVisible = false
            this.currentPage = 1
            this.getBannerList(this.currentPage)
          })
          .catch(error => {
            console.log(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    
    edit(id,scope){
      this.title = '修改'
      this.isAdd = false
      this.id = scope.row.id
      this.form = {
        title: scope.row.title,
        url: scope.row.url,
        img: scope.row.img,
        weight: scope.row.weight,
        type : scope.row.type
      }
      this.dialogFormVisible = true
    },
    
    toggle(id,scope){
      this.id = scope.row.id
      this.form = {
        title: scope.row.title,
        url: scope.row.url,
        img: scope.row.img,
        weight: scope.row.weight,
        type : 1
      }
      this.renewa()
    },
    
    renewa(){
      let pra = this.form
      pra.id = this.id
      this.$axios
      .post(`${this.$apiUrl}/gamemall/banner/update`, pra)
      .then(res => {
        this.dialogFormVisible = false
        this.getBannerList(this.currentPage)
      })
      .catch(error => {
        console.log(error);
      });
    }, 

    update(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.renewa()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    del(id,scope){
      console.log(id)
    } 
  },
  created(){
    this.getBannerList(this.currentPage)
  }
};
</script>
<style>

</style>
